package flower.com.a600_xm.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import flower.com.a600_xm.R;
import flower.com.a600_xm.adapter.ContactAdapter;
import flower.com.a600_xm.bean.MyEvent;
import flower.com.a600_xm.db.Z_ShoppingInfoDao;
import flower.com.a600_xm.db.dbbean.Buy_Shopping;
import flower.com.a600_xm.db.dbbean.Life_Shopping;


public class F1_Fragment extends Fragment {

    View view;
    Z_ShoppingInfoDao z_shoppingInfoDao;
    ContactAdapter ca;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {

            view = View.inflate(getActivity(), R.layout.fragment_f5, null);
            ButterKnife.bind(this, view);
            EventBus.getDefault().register(this);
            RecyclerView recList = (RecyclerView) view.findViewById(R.id.cardList);
            recList.setHasFixedSize(true);
            GridLayoutManager llm = new GridLayoutManager(getActivity(), 4);
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            recList.setLayoutManager(llm);


            z_shoppingInfoDao = new Z_ShoppingInfoDao(getActivity());
            ca = new ContactAdapter(createList());
            recList.setAdapter(ca);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        return view;
    }

    public void onEventMainThread(MyEvent event) {

        if (event.getType().equals("query")) {
            String kucun = event.getName();
            chaxun(kucun);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        chaxun("");
    }

    private List<Life_Shopping> createList() {

//        List<ContactInfo> result = new ArrayList<ContactInfo>();
        List<Life_Shopping> testList = z_shoppingInfoDao.query();


        for  ( int  i  =   0 ; i  <  testList.size()  -   1 ; i ++ )  {
            for  ( int  j  =  testList.size()  -   1 ; j  >  i; j -- )  {
                if  (testList.get(j).getName().equals(testList.get(i).getName()))  {
                    testList.remove(j);
                }
            }
        }
        for (int i = 0; i < testList.size(); i++) {
            Log.e("888", "xx"+testList.get(i).getName());

        }


        return testList;
    }


    public void chaxun(String name) {
        if (name.equals("")) {
            List<Life_Shopping> query = z_shoppingInfoDao.query();
            for  ( int  i  =   0 ; i  <  query.size()  -   1 ; i ++ )  {
                for  ( int  j  =  query.size()  -   1 ; j  >  i; j -- )  {
                    if  (query.get(j).getName().equals(query.get(i).getName()))  {
                        query.remove(j);
                    }
                }
            }
            ca.clear();
            ca.addItem(query);

        } else {
            Life_Shopping query = z_shoppingInfoDao.querys(name);
            if (query != null) {
                ca.clear();
                ca.addItem(query);
            } else {
                Toast.makeText(getActivity(), "暂无该商品", Toast.LENGTH_SHORT).show();

            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);


    }
}